part of '../repository.dart';

const _kPasswordResetTokenUserIdKey = 'user_id';
const _kPasswordResetTokenValueKey = 'value';
const _kPasswordResetTokenExpirationTimeKey = 'expiration_time';

class _PasswordResetTokenDto {
  const _PasswordResetTokenDto._internal({
    required this.userId,
    required this.value,
    required this.expirationTimeDto,
  });

  final String userId;
  final String value;
  final _TimeDto expirationTimeDto;

  factory _PasswordResetTokenDto.fromJson(Map<String, dynamic> json) {
    final expirationTimeString = json[_kPasswordResetTokenExpirationTimeKey] as String;
    final expirationTimeDto = _TimeDto.fromString(expirationTimeString);

    return _PasswordResetTokenDto._internal(
      userId: json[_kPasswordResetTokenUserIdKey],
      value: json[_kPasswordResetTokenValueKey],
      expirationTimeDto: expirationTimeDto,
    );
  }

  factory _PasswordResetTokenDto.fromEntity(PasswordResetToken passwordResetToken) {
    final expirationTimeDto = _TimeDto.fromEntity(passwordResetToken.expirationTime);

    return _PasswordResetTokenDto._internal(
      userId: passwordResetToken.userId,
      value: passwordResetToken.value,
      expirationTimeDto: expirationTimeDto,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      _kPasswordResetTokenUserIdKey: userId,
      _kPasswordResetTokenValueKey: value,
      _kPasswordResetTokenExpirationTimeKey: expirationTimeDto.toString(),
    };
  }

  PasswordResetToken toEntity() {
    return PasswordResetToken(
      userId: userId,
      value: value,
      expirationTime: expirationTimeDto.toEntity(),
    );
  }
}
